package org.dtrd.modules.food.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.food.entity.bean.FoodMatchingDetail;
import org.dtrd.modules.food.entity.bean.FoodMatchingListInfo;
import org.dtrd.modules.food.entity.request.FoodMatchingPageRequest;
import org.dtrd.modules.food.service.IDtrdExtEmsYwFoodMatchingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 配餐三餐 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-09
 */
@Api(tags = "食物库")
@RestController
@RequestMapping("/100/16/")
public class FoodMatchingController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwFoodMatchingService foodMatchingService;

    @ApiOperation(value = "查询配餐三餐详情", response = FoodMatchingDetail.class)
    @GetMapping("1001632")
    @AutoLog(value="查询配餐三餐详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFoodMatching(@RequestParam Integer id){
        FoodMatchingDetail info = foodMatchingService.getFoodMatching(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "分页查询配餐三餐列表", response = FoodMatchingListInfo.class)
    @PostMapping("1001631")
    @AutoLog(value="查询配餐三餐列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFoodMatchingList(@RequestBody FoodMatchingPageRequest request){
        handlePageRequest(request);
        IPage<FoodMatchingListInfo> page = foodMatchingService.pageFoodMatching(request);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改配餐三餐")
    @PostMapping("1001633")
    @AutoLog(value="新增或修改配餐三餐", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateFoodMatching(@RequestBody FoodMatchingDetail dto){
        if (StrUtil.isBlank(dto.getMatchingName()) 
                || dto.getMatchingCategoryid() == null) {
            return error(STATE_PARAMS_ERROR);
        }
        boolean result = foodMatchingService.saveOrUpdateFoodMatching(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除配餐三餐")
    @DeleteMapping("1001634")
    @AutoLog(value="删除配餐三餐", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeFoodMatching(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = foodMatchingService.removeFoodMatching(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
